package com.spider.admin.service.client;

import com.spider.admin.cache.ServiceInfoCacheUtil;
import com.spider.admin.cache.ServiceLogSettingsCacheUtil;
import com.spider.admin.cache.ServiceSettingCacheUtil;
import com.spider.admin.exception.SpiderException;
import com.spider.admin.service.account.AccountPermsService;
import com.spider.admin.store.primary.mapper.ClientTenantMapper;
import com.spider.admin.store.primary.mapper.LogSettingsMapper;
import com.spider.admin.store.primary.mapper.ServiceMapper;
import com.spider.admin.store.primary.model.ClientTenantInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PublishClientTenantsService {
    @Autowired
    private ClientTenantMapper clientTenantMapper;

    @Autowired
    private AccountPermsService accountPermsService;

    @Autowired
    private ServiceSettingCacheUtil serviceSettingCacheUtil;

    @Autowired
    private ServiceInfoCacheUtil serviceInfoCacheUtil;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private LogSettingsMapper logSettingsMapper;

    @Autowired
    private ServiceLogSettingsCacheUtil serviceLogSettingsCacheUtil;

    public void putToCacheForPerms() throws SpiderException {
        List<ClientTenantInfo> rlist = clientTenantMapper.queryClientTenants();
        String prefixPart1 = "token-";
        serviceSettingCacheUtil.deleteByPatternKey(prefixPart1);
        if (rlist != null && rlist.size() > 0) {

            for (ClientTenantInfo clientTenantInfo : rlist) {
                serviceSettingCacheUtil.set(prefixPart1 + "ckey-" + clientTenantInfo.getClientKey(), clientTenantInfo.toString());
            }
        }

        Map<String, String> permsMap = accountPermsService.getAccountsPermsForCache();
        if (permsMap != null && !permsMap.isEmpty()) {
            prefixPart1 = "perms-";
            serviceSettingCacheUtil.deleteByPatternKey(prefixPart1);
            for (String key : permsMap.keySet()) {
                serviceSettingCacheUtil.set(prefixPart1 + key, permsMap.get(key));
            }
        }

        log.info("#####刷新服务权限配置缓存#####");
    }

    public void putToCacheForServiceInfo() throws SpiderException {
        List<Map<String, String>> result = serviceMapper.queryServiceToTenant();
        serviceInfoCacheUtil.clear();
        if (result != null && !result.isEmpty()) {
            for (Map<String, String> map : result) {
                serviceInfoCacheUtil.set(map.get(ServiceInfoCacheUtil.SERVICE_URL), map);
            }
        }
        log.info("#####刷新服务清单缓存#####");
    }

    public void putToCacheForLogSettingInfo() throws SpiderException {
        List<Map<String, String>> result = logSettingsMapper.getAllLogsSettings();
        serviceLogSettingsCacheUtil.clear();
        if (result != null && !result.isEmpty()) {
            Map<String, String> settings = null;
            for (Map<String, String> map : result) {
                settings = serviceLogSettingsCacheUtil.get(map.get(ServiceInfoCacheUtil.SERVICE_URL));
                if (settings == null) {
                    settings = new HashMap<>();
                }
                settings.put(map.get("setting_key"), map.get("setting_value"));
                serviceLogSettingsCacheUtil.set(map.get(ServiceInfoCacheUtil.SERVICE_URL), settings);
            }
        }
        log.info("#####刷新日志配置缓存#####");
    }
}

